package com.lys.controller;

import com.lys.entity.News;
import com.lys.entity.Product;
import com.lys.service.NewsService;
import com.lys.service.ProductCategoryService;
import com.lys.service.ProductService;
import com.lys.utils.Pager;
import com.lys.utils.ProductCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/29 23:50
 */
@RequestMapping("/Home")
@Controller
public class HomeController {

    private ProductService productService;
    private NewsService newsService;
    private ProductCategoryService productCategoryService;

    @Autowired
    public HomeController(ProductService productService, NewsService newsService, ProductCategoryService productCategoryService) {
        this.productService = productService;
        this.newsService = newsService;
        this.productCategoryService = productCategoryService;
    }

    /**
     * 商城主页的方法
     * @param request
     * @return
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        //查询商品分裂
        List<ProductCategoryVo> productCategoryVoList = productCategoryService.queryAllProductCategoryList();
        List<News> news = newsService.getAllNews(new Pager(5, 5, 1));
        //查询一楼
        for (ProductCategoryVo vo : productCategoryVoList) {
            List<Product> productList = productService.getProductList(1, 8, null, vo.getProductCategory().getId(), 1);
            //List<Product> productList = productService.getProductList(currentPageNo, pageSize, proName, categoryId, level)
            vo.setProductList(productList);
        }
        //封装返回
        request.setAttribute("productCategoryVoList", productCategoryVoList);
        request.setAttribute("news", news);
        return "/pre/index";
    }
}
